package ttc.mBackup.BackupAndRestoreSMS;

import java.util.List;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;


public class SMSQuery {
	private static String SMS_URI_SMS_ALL="content://sms/";
	private static String SMS_URI_SMS_INBOX="content://sms/inbox";
	private static String SMS_URI_SMS_OUTBOX="content://sms/outbox";
	private Context context;
	private List<SMSClass> lst_SMS;
	private SMSClass smsClass= new SMSClass();
	public SMSQuery(Context context)
	{
		super();
		this.context=context;
	}
	public Cursor getAllSMS()
	{	
		Uri uriSMS=Uri.parse("content://sms/");
		
		Cursor cursorSMS=context.getContentResolver().query(uriSMS, null, null, null, null);
		/*try
		{
			 cursorSMS= getContentResolver().query(uriSMS, null, null, null, null);
		}	
		catch(Exception ex)
		{
			Toast.makeText(this, ex.getMessage(), Toast.LENGTH_SHORT);
		}
		if(cursorSMS.getCount()>0)
			return cursorSMS;
		else 
			return null;*/
		return cursorSMS;
	}
	public List<SMSClass> getInboxSMS()
	{
		
		Uri smsUri= Uri.parse("content://sms/inbox");
		Cursor cursorInboxSMS = context.getContentResolver().query(smsUri, null, null, null, null);
		cursorInboxSMS.moveToFirst();
		do 
		{
			smsClass.id=cursorInboxSMS.getInt(0);
			smsClass.thread_id=cursorInboxSMS.getInt(1);
			smsClass.address=cursorInboxSMS.getString(2);
			smsClass.person=cursorInboxSMS.getInt(3);
			smsClass.date=cursorInboxSMS.getInt(4);
			smsClass.protocol=cursorInboxSMS.getInt(5);
			smsClass.read=cursorInboxSMS.getInt(6);
			smsClass.status=cursorInboxSMS.getInt(7);
			smsClass.type=cursorInboxSMS.getInt(8);
			smsClass.reply_path_present=cursorInboxSMS.getInt(9);
			smsClass.subject=cursorInboxSMS.getString(10);
			smsClass.body=cursorInboxSMS.getString(11);
			smsClass.service_center=cursorInboxSMS.getString(12);
			smsClass.locked=cursorInboxSMS.getInt(13);
			smsClass.error_code=cursorInboxSMS.getInt(14);
			smsClass.seen=cursorInboxSMS.getInt(15);
			
			lst_SMS.add(smsClass);
		}
		while(!cursorInboxSMS.isLast());
		return lst_SMS;
	}

	public Cursor getOutboxSMS()
	{
		Uri smsUri= Uri.parse(SMS_URI_SMS_OUTBOX);
		Cursor cursorOutboxSMS = context.getContentResolver().query(smsUri, null, null, null, null);
		return cursorOutboxSMS;
	}
	public void insertSMS(ContentValues values)
	{
		Uri smsUri= Uri.parse(SMS_URI_SMS_ALL);
		context.getContentResolver().insert(smsUri, values);
	
	}
	
}
